home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTAtom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  3.7 KB  |  163 lines

  1. /*            Atoms: Names to numbers            HTAtom.c
  2. **            =======================
  3. **
  4. **    Atoms are names which are given representative pointer values
  5. **    so that they can be stored more efficiently, and comparisons
  6. **    for equality done more efficiently.
  7. **
  8. **    Atoms are kept in a hash table consisting of an array of linked lists.
  9. **
  10. ** Authors:
  11. **    TBL    Tim Berners-Lee, WorldWideWeb project, CERN
  12. **    (c) Copyright CERN 1991 - See Copyright.html
  13. **
  14. */
  15. #include "HTUtils.h"
  16.  
  17. #define HASH_SIZE    101        /* Tunable */
  18. #include "HTAtom.h"
  19.  
  20. /*#include <stdio.h> included by HTUtils.h -- FM *//* joe@athena, TBL 921019 */
  21. #include <string.h>
  22.  
  23. #include "HTList.h"
  24.  
  25. #include "LYexit.h"
  26. #include "LYLeaks.h"
  27.  
  28. PRIVATE HTAtom * hash_table[HASH_SIZE];
  29. PRIVATE BOOL initialised = NO;
  30.  
  31. /*
  32.  *    To free off all atoms.
  33.  */
  34. static void free_atoms NOPARAMS;
  35.  
  36. /*
  37.  *    Alternate hashing function.
  38.  */
  39. #define HASH_FUNCTION(cp_hash) ((strlen(cp_hash) * *cp_hash) % HASH_SIZE)
  40.  
  41. PUBLIC HTAtom * HTAtom_for ARGS1(CONST char *, string)
  42. {
  43.     int hash;
  44.     CONST char * p;
  45.     HTAtom * a;
  46.     
  47.     /*        First time around, clear hash table
  48.     */
  49.     /*
  50.      *    Memory leak fixed.
  51.      *  05-29-94 Lynx 2-3-1 Garrett Arch Blythe
  52.      */
  53.     if (!initialised) {
  54.         int i;
  55.     for (i=0; i<HASH_SIZE; i++)
  56.         hash_table[i] = (HTAtom *) 0;
  57.     initialised = YES;
  58.     atexit(free_atoms);
  59.     }
  60.     
  61.     /*        Generate hash function
  62.     */
  63.     hash = HASH_FUNCTION(string);
  64.     
  65.     /*        Search for the string in the list
  66.     */
  67.     for (a=hash_table[hash]; a; a=a->next) {
  68.     if (0==strcasecomp(a->name, string)) {
  69.             /* if (TRACE) fprintf(stderr,
  70.             "HTAtom: Old atom %p for `%s'\n", a, string); */
  71.         return a;                /* Found: return it */
  72.     }
  73.     }
  74.     
  75.     /*        Generate a new entry
  76.     */
  77.     a = (HTAtom *)malloc(sizeof(*a));
  78.     if (a == NULL) outofmem(__FILE__, "HTAtom_for");
  79.     a->name = (char *)malloc(strlen(string)+1);
  80.     if (a->name == NULL) outofmem(__FILE__, "HTAtom_for");
  81.     strcpy(a->name, string);
  82.     a->next = hash_table[hash];        /* Put onto the head of list */
  83.     hash_table[hash] = a;
  84. /*    if (TRACE) fprintf(stderr, "HTAtom: New atom %p for `%s'\n", a, string); */
  85.     return a;
  86. }
  87.  
  88. static void free_atoms NOARGS    {
  89. /*
  90.  *    Purpose:    Free off all atoms.
  91.  *    Arguments:    void
  92.  *    Return Value:    void
  93.  *    Remarks/Portability/Dependencies/Restrictions:
  94.  *        To be used at program exit.
  95.  *    Revision History:
  96.  *        05-29-94    created Lynx 2-3-1 Garrett Arch Blythe
  97.  */
  98.     auto int i_counter;
  99.     HTAtom *HTAp_freeme;
  100.     /*
  101.      *    Loop through all lists of atoms.
  102.      */
  103.     for(i_counter = 0; i_counter < HASH_SIZE; i_counter++)    {
  104.         /*
  105.          *    Loop through the list.
  106.          */
  107.         while(hash_table[i_counter] != NULL)    {
  108.             /*
  109.               *    Free off atoms and any members.
  110.              */
  111.             HTAp_freeme = hash_table[i_counter];
  112.             hash_table[i_counter] = HTAp_freeme->next;
  113.             free(HTAp_freeme->name);
  114.             free(HTAp_freeme);
  115.         }
  116.     }
  117. }
  118.  
  119. PRIVATE BOOL mime_match ARGS2(CONST char *, name,
  120.                   CONST char *, templ)
  121. {
  122.     if (name && templ) {
  123.     static char *n1 = NULL;
  124.     static char *t1 = NULL;
  125.     char *n2;
  126.     char *t2;
  127.  
  128.     StrAllocCopy(n1, name);        /* These also free the ones    */
  129.     StrAllocCopy(t1, templ);    /* from previous call.        */
  130.  
  131.     if (!(n2 = strchr(n1, '/'))  ||  !(t2 = strchr(t1, '/')))
  132.         return NO;
  133.  
  134.     *(n2++) = (char)0;
  135.     *(t2++) = (char)0;
  136.  
  137.     if ((0==strcmp(t1, "*") || 0==strcmp(t1, n1)) &&
  138.         (0==strcmp(t2, "*") || 0==strcmp(t2, n2)))
  139.         return YES;
  140.     }
  141.     return NO;
  142. }
  143.     
  144.  
  145. PUBLIC HTList *HTAtom_templateMatches ARGS1(CONST char *, templ)
  146. {
  147.     HTList *matches = HTList_new();
  148.  
  149.     if (initialised && templ) {
  150.     int i;
  151.     HTAtom *cur;
  152.  
  153.     for (i=0; i<HASH_SIZE; i++) {
  154.         for (cur = hash_table[i];  cur;  cur=cur->next) {
  155.         if (mime_match(cur->name, templ))
  156.             HTList_addObject(matches, (void*)cur);
  157.         }
  158.     }
  159.     }
  160.     return matches;
  161. }
  162.  
  163.